﻿using Elasticsearch.Net;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IConnection = RabbitMQ.Client.IConnection;

namespace LeafCore.Components.RabbitMQ
{
    public class RabbitMQContext
    {
        private static ConnectionFactory? factory;
        private static readonly object lockObj = new();

        /// <summary>
        /// 获取单个RabbitMQ连接
        /// </summary>
        /// <returns></returns>
        public static IConnection GetConnection(string hostName, int port, string userName, string password)
        {
            if (factory == null)
            {
                lock (lockObj)
                {
                    factory ??= new ConnectionFactory
                    {
                        HostName = hostName,
                        Port = port,
                        UserName = userName,
                        Password = password
                    };
                }
            }

            return factory.CreateConnection();
        }
    }
}
